﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace bstest
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            dataIO.InitializationData();//程序打开时初始化
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SectionData_Click(object sender, EventArgs e)
        {
            FormSectionData SectionData = new FormSectionData();
            SectionData.Show();
        }//打开输入参数页面

        private void buttonCal_Click(object sender, EventArgs e)
        {
            int calMode, number;
            double length, elasticity, density;
            string filename = "data.xml";
            dataIO.ImpellerData[] data = new dataIO.ImpellerData[51];
            dataIO.ReadData(filename,out calMode, out data, out number, out length, out elasticity, out density);
            cal.calmain(calMode, data, number, length, elasticity, density);
            MessageBox.Show("计算完成");
        }//读入数据并进行计算

        private void buttonAnsShow_Click(object sender, EventArgs e)
        {
            XDocument xdoc = XDocument.Load("data.xml");
            int calMode = int.Parse(xdoc.Element("root").Element("calMode").Value);
            if (calMode<3)
            {
                FormAnsShowWithShape AnsShow = new FormAnsShowWithShape();
                AnsShow.Show();
            }
            else
            {
                FormAnsShowWithoutShape AnsShow = new FormAnsShowWithoutShape();
                AnsShow.Show();
            }
        }//分辨是否有振型，调用相应输出方法

        private void InputData_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            string caldir = "data.xml";
            dlg.InitialDirectory = caldir;
            dlg.Filter = "XML File (*.xml)|*.xml|All Files|*.*";//限定打开文件的类型
            if (dlg.ShowDialog()==DialogResult.OK)
            {
                string filename = dlg.FileName;
                int calMode, number;
                double length, density, elasticity;
                dataIO.ImpellerData[] data = new dataIO.ImpellerData[51];
                dataIO.ReadData(filename, out calMode, out data, out number, out length, out elasticity, out density);
                dataIO.WriteData(caldir, calMode, data, number, length, elasticity, density);
            }
        }//读入数据文件

        private void OutputData_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            string caldir = "data.xml";
            dlg.InitialDirectory = caldir;
            dlg.Filter = "XML File (*.xml)|*.xml|All Files|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string filename = dlg.FileName;
                int calMode, number;
                double length, density, elasticity;
                dataIO.ImpellerData[] data = new dataIO.ImpellerData[51];
                dataIO.ReadData(caldir, out calMode, out data, out number, out length, out elasticity, out density);
                dataIO.WriteData(filename, calMode, data, number, length, elasticity, density);
            }
        }//写入数据文件
    }
}
